package com.urea.system.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.urea.system.domain.vo.CustomerAccountVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.urea.system.mapper.CustomerAccountMapper;
import com.urea.system.domain.CustomerAccount;
import com.urea.system.service.ICustomerAccountService;

/**
 * 客户/车队账户Service业务层处理
 *
 * @author liuzd
 * @date 2023-08-01
 */
@Service
public class CustomerAccountServiceImpl extends ServiceImpl<CustomerAccountMapper, CustomerAccount> implements ICustomerAccountService {
    @Autowired
    private CustomerAccountMapper customerAccountMapper;

    /**
     * 查询客户/车队账户
     *
     * @param id 客户/车队账户主键
     * @return 客户/车队账户
     */
    @Override
    public CustomerAccount selectCustomerAccountById(Long id) {
        return customerAccountMapper.selectCustomerAccountById(id);
    }

    /**
     * 查询客户/车队账户列表
     *
     * @param customerAccount 客户/车队账户
     * @return 客户/车队账户
     */
    @Override
    public List<CustomerAccount> selectCustomerAccountList(CustomerAccount customerAccount) {
        return customerAccountMapper.selectCustomerAccountList(customerAccount);
    }

    /**
     * 新增客户/车队账户
     *
     * @param customerAccount 客户/车队账户
     * @return 结果
     */
    @Override
    public int insertCustomerAccount(CustomerAccount customerAccount) {
        return customerAccountMapper.insertCustomerAccount(customerAccount);
    }

    /**
     * 修改客户/车队账户
     *
     * @param customerAccount 客户/车队账户
     * @return 结果
     */
    @Override
    public int updateCustomerAccount(CustomerAccount customerAccount) {
        return customerAccountMapper.updateCustomerAccount(customerAccount);
    }

    /**
     * 批量删除客户/车队账户
     *
     * @param ids 需要删除的客户/车队账户主键
     * @return 结果
     */
    @Override
    public int deleteCustomerAccountByIds(Long[] ids) {
        return customerAccountMapper.deleteCustomerAccountByIds(ids);
    }

    /**
     * 删除客户/车队账户信息
     *
     * @param id 客户/车队账户主键
     * @return 结果
     */
    @Override
    public int deleteCustomerAccountById(Long id) {
        return customerAccountMapper.deleteCustomerAccountById(id);
    }

    /**
     * 根据id更新账户余额
     *
     * @param newBalance 更新后余额
     * @param id         id
     */
    @Override
    public void updateBalanceById(BigDecimal newBalance, Long id) {
        CustomerAccount update = new CustomerAccount();
        update.setId(id);
        update.setBalance(newBalance);
        this.updateById(update);
    }

    @Override
    public CustomerAccountVo getFleetBalanceByUid(Long userId) {
        return baseMapper.getFleetBalanceByUid(userId);
    }

    @Override
    public CustomerAccountVo getByCustomerId(Long customerId) {
        CustomerAccount account = lambdaQuery().eq(CustomerAccount::getCustomerId, customerId).one();
        CustomerAccountVo vo = new CustomerAccountVo();
        BeanUtils.copyProperties(account,vo);
        return vo;
    }

    @Override
    public void updateBlanceByCustomerId(Long customerId, BigDecimal amount,Boolean fleetPay) {
        CustomerAccountVo account = fleetPay?getFleetBalanceByUid(customerId):getByCustomerId(customerId);
        if (Objects.nonNull(account)){
            CustomerAccount entity = new CustomerAccount();
            BeanUtils.copyProperties(account,entity);
            entity.setBalance(entity.getBalance().add(amount));
            saveOrUpdate(entity);
        }
    }
    @Override
    public CustomerAccountVo getByCustomerId(Long customerId, Boolean fleetPay) {
        return fleetPay?getFleetBalanceByUid(customerId):getByCustomerId(customerId);
    }
}
